package org.acghub.config;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/24/星期四 11:08
 * @Version 1.0
 */
public class TableMeta implements TypeParseable {

    private final String module;
    private final String tableName;
    private final TableHeader header;

    private boolean parse;

    public TableMeta(String module, String tableName, TableHeader header) {
        this.module = module;
        this.tableName = tableName;
        this.header = header;
    }

    public String getModule() {
        return module;
    }

    public String getTableName() {
        return tableName;
    }

    public TableHeader getHeader() {
        return header;
    }

    @Override
    public String toString() {
        return "TableMate{" + "module='" + module + '\'' + ", tableName='" + tableName + '\'' + ", header=" + header + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        TableMeta tableMeta = (TableMeta) o;
        return Objects.equals(module, tableMeta.module) && Objects.equals(tableName, tableMeta.tableName) && Objects.equals(header, tableMeta.header);
    }

    @Override
    public int hashCode() {
        return Objects.hash(module, tableName, header);
    }

    public String getPackageName() {
        return TableConfig.getPackageName(this);
    }

    public String getClassName() {
        return TableConfig.getClassName(this);
    }

    public Optional<TableField> getIdField() {
        return header.get("id");
    }

    public String getIdType() {
        return getIdField().get().getDeclareType();
    }

    @Override
    public void parse() throws TypeParseException {
        if (parse) {
            return;
        }
        header.parse();
        parse = true;
    }

    public Set<Integer> getColumnIndexes() {
        return header.getColumnIndexes();
    }

    public boolean hasColumnIndex(int index) {
        return getColumnIndexes().contains(index);
    }

}
